#include "ControlLayer.h"

ControlLayer::ControlLayer(void)
{
}

ControlLayer::~ControlLayer(void)
{
}

bool ControlLayer::init()
{
    bool bRet = false;
    do 
    {		
        CC_BREAK_IF(! CCLayer::init());

        CCMenuItemImage *pCloseItem = CCMenuItemImage::create("CloseNormal.png","CloseSelected.png",this,menu_selector(ControlLayer::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);
        this->addChild(pMenu, 1);

		CCMenuItem *heroDown = CCMenuItemFont::create("down", this, menu_selector(ControlLayer::menuCallBackMove));
		CCMenuItem *heroLeft = CCMenuItemFont::create("left", this, menu_selector(ControlLayer::menuCallBackMove));
		CCMenuItem *heroRight = CCMenuItemFont::create("right", this, menu_selector(ControlLayer::menuCallBackMove));
		CCMenuItem *heroUp = CCMenuItemFont::create("up", this, menu_selector(ControlLayer::menuCallBackMove));
		CCMenu *menu = CCMenu::create(heroDown,heroLeft,heroRight,heroUp,NULL);

		heroDown->setTag(gDOWN);
		heroLeft->setTag(gLEFT);
		heroRight->setTag(gRIGHT);
		heroUp->setTag(gUP);

		menu->alignItemsHorizontallyWithPadding(50);
		addChild(menu);

		schedule(schedule_selector(GameLayer::update));
        bRet = true;
    } while (0);

    return bRet;
}

void ControlLayer::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}

void ControlLayer::menuCallBackMove(CCObject* pSender)
{
	CCNode *node = (CCNode *)pSender;
	int di = node->getTag();
	sGlobal->hero->heroDi = (Direction)di;
	sGlobal->hero->heroMove();
}